package com.kpx.app.wigdet;

import android.content.Context;
import android.content.DialogInterface;


/**
 * 对话框工厂默认实现
 *
 * @author Scott Smith  @Date 2016年04月16/4/18日 17:11
 */
public class DialogFactoryImpl extends DialogFactory {
    private Context mContext;
    private GreenDialog mAlertDialog;
    private GreenDialog mConfirmDialog;
    private AddressMentionDialog addressDialog;
    private LoadingDialog mLoadingDialog;
    private AMentionDialog aDialog;

    public DialogFactoryImpl(Context context) {
        mContext = context;
    }

    @Override
    public void showAlertDialog(String text) {
        if (null == mAlertDialog) {
            mAlertDialog = new GreenDialog.Builder(mContext)
                    .cancelable(true)
                    .setContent(text)
                    .create();
        }
        mAlertDialog.setContent(text);
        if (!mAlertDialog.isShowing()) mAlertDialog.show();
    }

    @Override
    public void showConfirmDialog(String negativeButtonText, String positiveButtonText, String text, DialogInterface.OnClickListener onClickListener) {
        if (null == mConfirmDialog) {
            mConfirmDialog = new GreenDialog.Builder(mContext)
                    .cancelable(false)
                    .setPositiveButton(positiveButtonText)
                    .setNegativeButton(negativeButtonText)
                    .onClickListener(onClickListener)
                    .create();
        }
        mConfirmDialog.setContent(text);
        if (!mConfirmDialog.isShowing()) {
            mConfirmDialog.show();
        }
    }

    @Override
    public void showConfirmDialog(String negativeButtonText, String positiveButtonText, String title, String text, DialogInterface.OnClickListener onClickListener) {
        if (null == mConfirmDialog) {
            mConfirmDialog = new GreenDialog.Builder(mContext)
                    .cancelable(false)
                    .setPositiveButton(positiveButtonText)
                    .setNegativeButton(negativeButtonText)
                    .onClickListener(onClickListener)
                    .setTitle(title)
                    .create();
        }
        mConfirmDialog.setContent(text);

        if (!mConfirmDialog.isShowing()) {
            mConfirmDialog.show();
        }
    }
    @Override
    public void showConfirmDialog(String negativeButtonText, String positiveButtonText, String title, String text, DialogInterface.OnClickListener onClickListener,boolean canCancel) {
        if (null == mConfirmDialog) {
            mConfirmDialog = new GreenDialog.Builder(mContext)
                    .cancelable(canCancel)
                    .setPositiveButton(positiveButtonText)
                    .setNegativeButton(negativeButtonText)
                    .onClickListener(onClickListener)
                    .setTitle(title)
                    .create();
        }
        mConfirmDialog.setContent(text);

        if (!mConfirmDialog.isShowing()) {
            mConfirmDialog.show();
        }
    }

    @Override
    public void showAddressMentionDialog(String negativeButtonText, String positiveButtonText, String title, AddressMentionDialog.OnNegativeButtonClickListener onNegativeButtonClickListener) {
        if (null == addressDialog) {
            addressDialog = new AddressMentionDialog(mContext, negativeButtonText, positiveButtonText, title, onNegativeButtonClickListener);
        }
        if (!addressDialog.isShowing()) {
            addressDialog.show();
        }
    }

    @Override
    public void showAMentionDialog(String negativeButtonText, String positiveButtonText, String title, String mg, AMentionDialog.OnNegativeButtonClickListener onNegativeButtonClickListener) {
        if (null == aDialog) {
            aDialog = new AMentionDialog(mContext, negativeButtonText, positiveButtonText, title, mg, onNegativeButtonClickListener);
        }
        if (!aDialog.isShowing()) {
            aDialog.show();
        }
    }

    @Override
    public void showWarningDialog(String text) {

    }

    @Override
    public void showLoadingDialog(String text) {
        if (null == mLoadingDialog) {
            mLoadingDialog = new LoadingDialog(mContext);
        }
        mLoadingDialog.setTitle(text);
        if (!mLoadingDialog.isShowing()) mLoadingDialog.show();
    }

    @Override
    public void dismissDialog() {
        if (null != mAlertDialog && mAlertDialog.isShowing()) mAlertDialog.dismiss();
        if (null != mConfirmDialog && mConfirmDialog.isShowing()) mConfirmDialog.dismiss();
        if (null != mLoadingDialog && mLoadingDialog.isShowing()) mLoadingDialog.dismiss();
        if (null != addressDialog && addressDialog.isShowing()) addressDialog.dismiss();
        if (null != aDialog && aDialog.isShowing()) aDialog.dismiss();
    }

    public boolean isDialogShow(){
        if (null != mAlertDialog && mAlertDialog.isShowing()) return true;
        if (null != mConfirmDialog && mConfirmDialog.isShowing()) return true;
        if (null != mLoadingDialog && mLoadingDialog.isShowing()) return true;
        if (null != addressDialog && addressDialog.isShowing()) return true;
        if (null != aDialog && aDialog.isShowing()) return true;
        return false;
    }
}
